﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Configuration;

namespace AFT.Parking.Foundation
{
    public class UrlFilter : IHttpModule
    {
        private static string OutLink =WebConfigurationManager.AppSettings["OutLink"];
        private static string GoLink = WebConfigurationManager.AppSettings["GoLink"];

        public void Init(HttpApplication application)
        {
            application.BeginRequest += Application_BeginRequest;
        }

        public void Dispose()
        {
        }

        private void Application_BeginRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            ReWriteUrl(application.Context);
        }

        private bool ReWriteUrl(HttpContext context)
        {
            Uri uri = context.Request.UrlReferrer;
            if (uri != null)
            {
                if (uri.ToString().ToLower().IndexOf(OutLink) > -1)
                {
                    context.Response.Redirect(GoLink);
                }
            }
            return true;
        }
    }
}
